<template>
	<view>
		
	</view>
</template>

<script>
	const DB = wx.cloud.database().collection('userList1')
	export default {
		data() {
			return {
				userName: '',
				headPic: '',
				_openid: '',
			}
		},
		created() {
			wx.cloud.callFunction({
								name: 'getOpenId',	 // 打开微信云开发控制平台，左上角点击[云函数]
								data: {},
								success: res => {
								  // debugger
								  // 第三步(可省略)：缓存用户openid，方便后续再次调用
								  wx.setStorage({
									key: "user",
									data: res.result.userInfo.openId
								  })
								  console.log('成功获取openid: ', res.result.userInfo.openId)

								 this._openid= res.result.userInfo.openId
								DB.where({_openid:this._openid}).get({
									success: (res) => {
										let list = res.data;
										console.log("adasdas",res)
										// uni.$emit('getId',list)
										if( list == ''){
											uni.showModal({
											title: '授权',
											content: "是否授权",
											success: () => {
												this.getWechatUserInfo()
											},
											fail: () => {
												wx.showToast({
													title: '登录失败！！',
													icon: 'none',
													duration: 2500
												})
											},
										})
										}else{
											let list = res.data[0]._id;
											uni.$emit('getId',list)
											uni.$emit('getOpenid',this._openid)
											// uni.showToast({
											//   title: "用户正在自动登录",
											//   icon: "loading",
											//   duration: 2000,
											// })
											this.show = false
										}									
										}
								})
								},
								fail: err => {
								  console.error('获取失败：', err)
								}
			})
	
			
			
			

		},
		
		methods: {
		getWechatUserInfo() {
				uni.getUserProfile({
					desc: "获取你的昵称、头像、地区及性别",
					success: (res) => {
						this.getUserInfoTag = false
						let userName = res.userInfo.nickName
						let headPic = res.userInfo.avatarUrl
						console.log("所有", res.userInfo)
						DB.where({
							_openid: this._openid
						}).get({
							success(res) {
								console.log("111111")
								console.log(res)
								if (res.data == '') {
									DB.add({
										data: {
											userName: userName,
											headPic: headPic,
										},
										success(res) {
											console.log("添加成功", res)
										},
										fail(res) {
											console.log("添加失败", res)
										}
									})
								}
							}
						})

						uni.$emit('getOpenId', this._openid)
						uni.$emit("getlogin", this.userName)
					},
					fail() {}
				})

			},
		},
		// 微信授权
	}
</script>

<style>

</style>
